package beans;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import sun.org.mozilla.javascript.internal.json.JsonParser.ParseException;
import dto.GovernorDTO;
import entitymanagers.AnnounceManager;
import entitymanagers.GovernorManager;

@ManagedBean(name = "announceGovernor")
@RequestScoped
public class GovernorBean {
		@EJB
		private GovernorManager govMgr;
		@EJB
		private AnnounceManager announceMgr;
		private GovernorDTO govDTO;
		
		private String dateElection;
		private String startCandidacy;
		private String endCandidacy;
		
		
		public GovernorManager getGovernorManager(){
			return govMgr;
		}
		public void setGovernorManager(GovernorManager govMgr){
			this.govMgr = govMgr;
		}
		public GovernorDTO getGovDTO() {
			return govDTO;
		}
		public void setGovDTO(GovernorDTO govDTO) {
			this.govDTO = govDTO;
		}
		public String getDateElection() {
			return dateElection;
		}
		public void setDateElection(String dateElection) {
			this.dateElection = dateElection;
		}
		public String getStartCandidacy() {
			return startCandidacy;
		}
		public void setStartCandidacy(String startCandidacy) {
			this.startCandidacy = startCandidacy;
		}
		public String getEndCandidacy() {
			return endCandidacy;
		}
		public void setEndCandidacy(String endCandidacy) {
			this.endCandidacy = endCandidacy;
		}
		
		public GovernorBean(){
			govDTO = new GovernorDTO();		
			}
		
		public GovernorDTO getGovernorDTO(){
			if (govDTO == null){
				govDTO = new GovernorDTO();
			}
			return govDTO;
		}
		public void setGovernorDTO(GovernorDTO govDTO){
			this.govDTO = govDTO;
			
		}
		
		public void setCandidacyWindow(){
			Date start = null;
			Date end = null;
			try{
				start = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH).parse(startCandidacy);
				end = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH).parse(endCandidacy);
			}
			catch(java.text.ParseException e){
				e.printStackTrace();
			}
			announceMgr.setCandidacyPeriod(start, end);
		
		}
		
		public void setDateElection(){
			Date dateElect = null;
			try{
				dateElect = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH).parse(dateElection);
				
			}
			catch (java.text.ParseException e){
				e.printStackTrace();
			}
			
			announceMgr.setDateElection(dateElect);
		}
}
